package com.gitee.huxing.course.core.aspect;

import com.gitee.huxing.course.core.annoation.DataSource;
import com.gitee.huxing.course.core.datasource.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description: 数据源切换扫描
 * @ProjectName: SpringCloudLearning
 * @Package: com.huxing.user.core.aspect
 * @ClassName: DataSourceAspect
 * @Author: huxing
 * Date: 2021-05-22
 * Time: 19:11
 */
@Component
@Slf4j
@Aspect
@Order(2)
public class DataSourceAspect {

    /**
     * @Description: 切换
     * @Author: huxing
     * @Date: 2021-05-22 19:13
     * @return: void
     **/
    @Pointcut("@within(com.gitee.huxing.course.core.annoation.DataSource) " +
            "|| @annotation(com.gitee.huxing.course.core.annoation.DataSource)")
    public void pointCut(){
    }

    @Before("pointCut() && @annotation(dataSource)")
    public void dobefore(DataSource dataSource){
        log.info("选择数据源---"+dataSource.value().getValue());
        DataSourceContextHolder.setDataSource(dataSource.value().getValue());
    }

    @After("pointCut()")
    public void doAfter(){
        DataSourceContextHolder.clear();
    }
}
